package com.soft.demo.service.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.soft.dao.GenericDao;
import com.soft.demo.service.IDemoService;
import com.soft.mapping.Demo;
import com.soft.mapping.User;

public class DemoServiceImpl extends GenericDao<Demo> implements IDemoService {

	@Override
	public int getCountDemo() {

		Session session = this.getSession();
		int count = super.getCountByCriteria(session.createCriteria(getPojoClass()));
		return count;

		// return (Integer) this.getHibernateTemplate().execute(new HibernateCallback<Object>() {
		// @Override
		// public Object doInHibernate(Session session) throws HibernateException, SQLException {
		// Criteria cr = session.createCriteria(getPojoClass());
		// @SuppressWarnings("rawtypes")
		// List list = cr.list();
		// if (list != null) {
		// return list.size();
		// }
		// return 0;
		// }
		// });

	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Demo> getDemoList(int pn, int rn) {
		Session session = this.getSession();
		Criteria criteria = session.createCriteria(getPojoClass());
		return super.findPageByCriteria(criteria, rn, (pn - 1) * rn).getItems();

		// return this.getHibernateTemplate().executeFind(new HibernateCallback<Object>() {
		// @Override
		// public Object doInHibernate(Session session) throws HibernateException, SQLException {
		// Criteria cr = session.createCriteria(getPojoClass());
		// cr.setFirstResult((pn - 1) * rn);
		// cr.setMaxResults(rn);
		// return cr.list();
		// }
		// });
	}

	@Override
	public List<Demo> getDemoList() {
		return super.loadAll();
	}

	@Override
	public void test() {
		Session session = this.getSession();
		int count = super.getCountByCriteria(session.createCriteria(Demo.class));
		int count2 = super.getCountByCriteria(session.createCriteria(User.class));
		System.out.println(count + "   " + count2);
	}

}
